/**
 * @file 工具集
 * @author yangjiangming <449654337@qq.com>
 * @since 2018/12/08 17:20:40
 */
layui.define(['layer'], function (exports) {
    /**
     * 获取数据的类型
     * @param {*} a 任何类型的变量
     * @returns {string}
     */
    function getDataType (a) {
        return Object.prototype.toString.call(a);
        // null --> [object Null]
        // undefined --> [object Undefined]
        // object --> [object Object]
        // array --> [object Array]
        // string --> [object String]
        // number --> [object Number]
        // function --> [object Function]
        // new Date --> [object Date]
        // Math --> [object Math]
        // Math.floor --> [object Function]
    }

    /**
     * 编码查询参数
     * @param {Object} data
     * @returns {String} url 编码的查询参数
     * @private
     */
    function _urlEncode (data) {
        var urlEncode = function (param, key, encode) {
            if (param==null) return '';
            var paramStr = '';
            var t = typeof (param);
            if (t == 'string' || t == 'number' || t == 'boolean') {
                paramStr += '&' + key + '='  + ((encode==null||encode) ? encodeURIComponent(param) : param);
            } else {
                for (var i in param) {
                    var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i)
                    paramStr += urlEncode(param[i], k, encode)
                }
            }
            return paramStr;
        }

        return urlEncode(data).slice(1);
    }

    /**
     * 序列化 url 地址
     * @param url 基础 url
     * @param {Object} params 查询参数
     * @returns {string} 序列化后的 url 字符串
     */
    function _serializeURL (url, params) {
        return url + '?' + _urlEncode(params);
    }

    /**
     * 获取当前 URL 里某一个查询参数的值
     * @param {String} name 参数名
     * @returns {*}
     * @private
     */
    function _getQueryString (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    /**
     * 获取当前 URL 的查询参数
     * @returns {Object}
     * @private
     */
    function _getQueryParams (url) {
        var r = url ? url.split('?')[1] : window.location.search.substr(1);
        var params = {};
        if (r && r.length) {
            var arr = r.split('&');
            arr.forEach((item) => {
                var ar = item.split('=');
                params[decodeURIComponent(ar[0])] = decodeURIComponent(ar[1]);
            });
        }

        return params;
    }

    /**
     * 导出文件
     * @param url 导出 url
     * @param params 导出参数
     * @private
     */
    function _exportFile (url, params) {
        var exportURL = _serializeURL(url, params);
        window.open(exportURL, '_blank');
    }

    var utils = {
        isNull: function (a) { return getDataType(a) === '[object Null]'; },
        isUndefined: function (a) { return getDataType(a) === '[object Undefined]'; },
        isObect: function (a) { return getDataType(a) === '[object Object]'; },
        isArray: function (a) { return getDataType(a) === '[object Array]'; },
        isString: function (a) { return getDataType(a) === '[object String]'; },
        isNumber: function (a) { return getDataType(a) === '[object Number]'; },
        isFunction: function (a) { return getDataType(a) === '[object Function]'; },
        isEmpty: function (a) {},
        urlEncode: _urlEncode,
        serializeURL: _serializeURL,
        getQueryString: _getQueryString,
        getQueryParams: _getQueryParams,
        exportFile: _exportFile
    };

    exports('zmnUtils', utils);
});